load packages

##load merged file 
fileNam <- "/Users/immbio/Desktop/HumanHeartCarTrans2/data/Human_heart_allmerged_seurat.rds"
seuratM <- readRDS(fileNam)

output_path <- "/Users/immbio/Desktop/HumanHeartCarTrans2/analysis/Preprocessing"

Using scDblFinder (https://github.com/plger/scDblFinder) as this approach is outperforming DoubletFinder. Original Paper Publikation: https://f1000research.com/articles/10-979/v2

sce <- as.SingleCellExperiment(seuratM)
## Warning: `PackageCheck()` was deprecated in SeuratObject 5.0.0.
## ℹ Please use `rlang::check_installed()` instead.
## ℹ The deprecated feature was likely used in the Seurat package.
##   Please report the issue at <https://github.com/satijalab/seurat/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The `slot` argument of `GetAssayData()` is deprecated as of SeuratObject 5.0.0.
## ℹ Please use the `layer` argument instead.
## ℹ The deprecated feature was likely used in the Seurat package.
##   Please report the issue at <https://github.com/satijalab/seurat/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
#Set seed to ensure reproducibility
seed <- 1234
bp <- MulticoreParam(3, RNGseed=1234)
sce <- scDblFinder(sce, samples = "Sample", BPPARAM = bp)
table(sce$scDblFinder.class)
## 
## singlet doublet 
##  186338   13419
meta_scdblfinder <- sce@colData@listData%>% as.data.frame()%>%
  dplyr::select(starts_with("scDblFinder"))%>%
  dplyr::select(scDblFinder.class, scDblFinder.score)
rownames(meta_scdblfinder) <- sce@colData@rownames

seuratM <- AddMetaData(seuratM, metadata = meta_scdblfinder)
table(seuratM$Sample, seuratM$scDblFinder.class)
##                                                                                                                                             
##                                                                                                                                              singlet
##   /data/mapped/NovaSeq_20220609_NOV1312_o28576_DataDelivery/o28576_1_08-8_20220525_Hu_nucseq_Graz_8_HH_GEM/outs/filtered_feature_bc_matrix      3765
##   /data/mapped/NovaSeq_20220609_NOV1312_o28576_DataDelivery/o28576_1_10-10_20220525_Hu_nucseq_Graz_10_HH_GEM/outs/filtered_feature_bc_matrix    3679
##   /data/mapped/NovaSeq_20220609_NOV1312_o28576_DataDelivery/o28576_1_11-11_20220525_Hu_nucseq_Graz_11_HH_GEM/outs/filtered_feature_bc_matrix    3986
##   /data/mapped/NovaSeq_20220609_NOV1312_o28576_DataDelivery/o28576_1_12-12_20220525_Hu_nucseq_Graz_12_HH_GEM/outs/filtered_feature_bc_matrix    3653
##   /data/mapped/NovaSeq_20220901_NOV1421_o29273_DataDelivery/o292731_1-1_20220818_Hu_nucseq_Graz_9_HH_GEM/outs/filtered_feature_bc_matrix        5918
##   /data/mapped/NovaSeq_20220901_NOV1421_o29273_DataDelivery/o292731_2-2_20220818_Hu_nucseq_Graz_13_HH_GEM/outs/filtered_feature_bc_matrix      10342
##   /data/mapped/NovaSeq_20220916_NOV1443_o29478_DataDelivery/o294781_01-1_20220912_Hu_nucseq_Graz_21_HH_GEM/outs/filtered_feature_bc_matrix      1405
##   /data/mapped/NovaSeq_20220916_NOV1443_o29478_DataDelivery/o294781_02-2_20220912_Hu_nucseq_Graz_22_HH_GEM/outs/filtered_feature_bc_matrix      1977
##   /data/mapped/NovaSeq_20220916_NOV1443_o29478_DataDelivery/o294781_03-3_20220912_Hu_nucseq_Graz_23_HH_GEM/outs/filtered_feature_bc_matrix       831
##   /data/mapped/NovaSeq_20220916_NOV1443_o29478_DataDelivery/o294781_04-4_20220912_Hu_nucseq_Graz_24_HH_GEM/outs/filtered_feature_bc_matrix      2068
##   /data/mapped/o33157_NovaSeq_231024_NOV1906/331571_3-5_20231012_Hu_nucseq_USZ_HTx001/outs/filtered_feature_bc_matrix                           3173
##   /data/mapped/o33157_NovaSeq_231024_NOV1906/331571_4-6_20231012_Hu_nucseq_USZ_HTx002/outs/filtered_feature_bc_matrix                           2388
##   /data/mapped/o33413_NovaSeq_231120_NOV1928/334131_01-1_20231103_Hu_nucseq_USZ_HTx002_RV/outs/filtered_feature_bc_matrix                       4205
##   /data/mapped/o33413_NovaSeq_231120_NOV1928/334131_02-2_20231103_Hu_nucseq_USZ_HTx003_LV/outs/filtered_feature_bc_matrix                       3870
##   /data/mapped/o33413_NovaSeq_231120_NOV1928/334131_03-3_20231103_Hu_nucseq_USZ_HTx004_RV/outs/filtered_feature_bc_matrix                       3900
##   /data/mapped/o33413_NovaSeq_231120_NOV1928/334131_04-4_20231106_Hu_nucseq_USZ_HTx003_RV/outs/filtered_feature_bc_matrix                       3679
##   /data/mapped/o33413_NovaSeq_231120_NOV1928/334131_05-5_20231106_Hu_nucseq_USZ_HTx004_LV/outs/filtered_feature_bc_matrix                       4833
##   /data/mapped/o33413_NovaSeq_231120_NOV1928/334131_06-6_20231106_Hu_nucseq_USZ_HTx005_RV/outs/filtered_feature_bc_matrix                       2758
##   /data/mapped/o33413_NovaSeq_231120_NOV1928/334131_07-7_20231107_Hu_nucseq_USZ_HTx005_LV/outs/filtered_feature_bc_matrix                       1787
##   /data/mapped/o33413_NovaSeq_231120_NOV1928/334131_08-8_20231107_Hu_nucseq_USZ_HTx006_RV/outs/filtered_feature_bc_matrix                       2408
##   /data/mapped/o33413_NovaSeq_231120_NOV1928/334131_09-9_20231108_Hu_nucseq_USZ_HTx006_LV/outs/filtered_feature_bc_matrix                       3290
##   /data/mapped/o33413_NovaSeq_231120_NOV1928/334131_10-10_20231108_Hu_nucseq_USZ_HTx001_RV/outs/filtered_feature_bc_matrix                      2022
##   /data/mapped/o33643_NovaSeq_231207_NOV1935/336431_07-7_20231129_Hu_nucseq_USZ_HTx008_RV/outs/filtered_feature_bc_matrix                       4841
##   /data/mapped/o33643_NovaSeq_231207_NOV1935/336431_08-8_20231129_Hu_nucseq_USZ_EMB001_V1/outs/filtered_feature_bc_matrix                        491
##   /data/mapped/o33643_NovaSeq_231207_NOV1935/336431_09-9_20231129_Hu_nucseq_USZ_HTx008_LV/outs/filtered_feature_bc_matrix                       3660
##   /data/mapped/o33643_NovaSeq_231207_NOV1935/336431_10-10_20231129_Hu_nucseq_USZ_EMB005_V1/outs/filtered_feature_bc_matrix                      1083
##   /data/mapped/o33643_NovaSeq_231207_NOV1935/336431_11-11_20231129_Hu_nucseq_USZ_EMB006_V1/outs/filtered_feature_bc_matrix                       479
##   /data/mapped/o33643_NovaSeq_231207_NOV1935/336431_12-12_20231129_Hu_nucseq_USZ_EMB007_V1/outs/filtered_feature_bc_matrix                      1456
##   /data/mapped/o33643_NovaSeq_231207_NOV1935/336431_13-13_20231129_Hu_nucseq_USZ_EMB002_V1/outs/filtered_feature_bc_matrix                      1841
##   /data/mapped/o33643_NovaSeq_231207_NOV1935/336431_14-14_20231129_Hu_nucseq_USZ_EMB003_V1/outs/filtered_feature_bc_matrix                      1878
##   /data/mapped/o33643_NovaSeq_231207_NOV1935/336431_15-15_20231129_Hu_nucseq_USZ_EMB004_V1/outs/filtered_feature_bc_matrix                      1595
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_01-7_20240118_Hu_nucseq_USZ_EMB007_V2/outs/filtered_feature_bc_matrix                       1748
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_02-8_20240118_Hu_nucseq_USZ_EMB008_V2/outs/filtered_feature_bc_matrix                        570
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_03-9_20240123_Hu_nucseq_USZ_EMB001_V3/outs/filtered_feature_bc_matrix                         66
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_04-10_20240123_Hu_nucseq_USZ_EMB002_V3/outs/filtered_feature_bc_matrix                       338
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_05-11_20240123_Hu_nucseq_USZ_EMB003_V3/outs/filtered_feature_bc_matrix                       164
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_06-12_20240123_Hu_nucseq_USZ_EMB004_V3/outs/filtered_feature_bc_matrix                       813
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_07-13_20240123_Hu_nucseq_USZ_EMB005_V3/outs/filtered_feature_bc_matrix                      1951
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_08-14_20240123_Hu_nucseq_USZ_EMB006_V3/outs/filtered_feature_bc_matrix                       235
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_09-15_20240123_Hu_nucseq_USZ_EMB007_V3/outs/filtered_feature_bc_matrix                       377
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_10-16_20240123_Hu_nucseq_USZ_EMB008_V3/outs/filtered_feature_bc_matrix                       182
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_11-17_20240123_Hu_nucseq_USZ_EMB008_V1/outs/filtered_feature_bc_matrix                        49
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_12-18_20240123_Hu_nucseq_USZ_HTx007_RV/outs/filtered_feature_bc_matrix                      2602
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_13-19_20240123_Hu_nucseq_USZ_HTx007_LV/outs/filtered_feature_bc_matrix                      2423
##   /data/mapped/o34083_NovaSeq_240206_NOV1953/340831_1-1_20240118_Hu_nucseq_USZ_EMB001_V2/outs/filtered_feature_bc_matrix                        1128
##   /data/mapped/o34083_NovaSeq_240206_NOV1953/340831_2-2_20240118_Hu_nucseq_USZ_EMB002_V2/outs/filtered_feature_bc_matrix                        2796
##   /data/mapped/o34083_NovaSeq_240206_NOV1953/340831_3-3_20240118_Hu_nucseq_USZ_EMB003_V2/outs/filtered_feature_bc_matrix                         404
##   /data/mapped/o34083_NovaSeq_240206_NOV1953/340831_4-4_20240118_Hu_nucseq_USZ_EMB004_V2/outs/filtered_feature_bc_matrix                         618
##   /data/mapped/o34083_NovaSeq_240206_NOV1953/340831_5-5_20240118_Hu_nucseq_USZ_EMB005_V2/outs/filtered_feature_bc_matrix                         694
##   /data/mapped/o34083_NovaSeq_240206_NOV1953/340831_6-6_20240118_Hu_nucseq_USZ_EMB006_V2/outs/filtered_feature_bc_matrix                         830
##   /data/mapped/o34774_NovaSeq_240411_NOV1972/347741_1-1_20240326_Hu_nucseq_USZ_HTx009_RV/outs/filtered_feature_bc_matrix                        3598
##   /data/mapped/o34774_NovaSeq_240411_NOV1972/347741_2-2_20240326_Hu_nucseq_USZ_HTx009_LV/outs/filtered_feature_bc_matrix                        6060
##   /data/mapped/o34774_NovaSeq_240411_NOV1972/347741_4-4_20240326_Hu_nucseq_USZ_HTx010_RV/outs/filtered_feature_bc_matrix                        2109
##   /data/mapped/o34774_NovaSeq_240411_NOV1972/347741_5-5_20240326_Hu_nucseq_USZ_HTx010_LV/outs/filtered_feature_bc_matrix                        2950
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_01-1_20240515_Hu_nucseq_USZ_HTx011_RV/outs/filtered_feature_bc_matrix                       1843
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_02-2_20240515_Hu_nucseq_USZ_HTx011_LV/outs/filtered_feature_bc_matrix                       1374
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_03-3_20240515_Hu_nucseq_USZ_EMB011_V1/outs/filtered_feature_bc_matrix                        799
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_04-4_20240515_Hu_nucseq_USZ_EMB011_V2/outs/filtered_feature_bc_matrix                        236
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_05-5_20240515_Hu_nucseq_USZ_HTx012_RV/outs/filtered_feature_bc_matrix                       1327
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_06-6_20240515_Hu_nucseq_USZ_HTx012_LV/outs/filtered_feature_bc_matrix                       2939
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_07-7_20240515_Hu_nucseq_USZ_EMB012_V1/outs/filtered_feature_bc_matrix                        796
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_08-8_20240515_Hu_nucseq_USZ_EMB010_V3/outs/filtered_feature_bc_matrix                        716
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_09-9_20240515_Hu_nucseq_USZ_HTx013_RV/outs/filtered_feature_bc_matrix                       1814
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_10-10_20240515_Hu_nucseq_USZ_HTx013_LV/outs/filtered_feature_bc_matrix                      2514
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_11-11_20240515_Hu_nucseq_USZ_EMB010_V1_1/outs/filtered_feature_bc_matrix                    1073
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_12-12_20240515_Hu_nucseq_USZ_EMB010_V1_2/outs/filtered_feature_bc_matrix                     256
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_13-13_20240524_Hu_nucseq_USZ_HTx015_RV/outs/filtered_feature_bc_matrix                      2105
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_14-14_20240524_Hu_nucseq_USZ_EMB011_V3/outs/filtered_feature_bc_matrix                       628
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_15-15_20240524_Hu_nucseq_USZ_EMB012_V2/outs/filtered_feature_bc_matrix                      2582
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_17-17_20240524_Hu_nucseq_USZ_HTx015_LV/outs/filtered_feature_bc_matrix                      1428
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_18-18_20240524_Hu_nucseq_USZ_EMB013_V1/outs/filtered_feature_bc_matrix                       520
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_19-19_20240524_Hu_nucseq_USZ_EMB013_V2/outs/filtered_feature_bc_matrix                       125
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_20-20_20240524_Hu_nucseq_USZ_EMB013_V3/outs/filtered_feature_bc_matrix                       389
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_21-21_20240524_Hu_nucseq_USZ_HTx014_RV/outs/filtered_feature_bc_matrix                      2384
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_22-22_20240524_Hu_nucseq_USZ_HTx014_LV/outs/filtered_feature_bc_matrix                      2434
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_23-23_20240524_Hu_nucseq_USZ_EMB014_V1/outs/filtered_feature_bc_matrix                      1061
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_24-24_20240524_Hu_nucseq_USZ_EMB014_V2/outs/filtered_feature_bc_matrix                       340
##   /data/mapped/o35986_NovaSeq_240814_NOV1996/359861_01-1_20240715_Hu_nucseq_USZ_HTx016_LV/outs/filtered_feature_bc_matrix                       5638
##   /data/mapped/o35986_NovaSeq_240814_NOV1996/359861_02-2_20240715_Hu_nucseq_USZ_HTx016_RV/outs/filtered_feature_bc_matrix                       6797
##   /data/mapped/o35986_NovaSeq_240814_NOV1996/359861_03-3_20240715_Hu_nucseq_USZ_EMB014_V3/outs/filtered_feature_bc_matrix                        419
##   /data/mapped/o35986_NovaSeq_240814_NOV1996/359861_04-4_20240715_Hu_nucseq_USZ_EMB015_V1/outs/filtered_feature_bc_matrix                        173
##   /data/mapped/o35986_NovaSeq_240814_NOV1996/359861_05-5_20240715_Hu_nucseq_USZ_EMB015_V2/outs/filtered_feature_bc_matrix                        200
##   /data/mapped/o35986_NovaSeq_240814_NOV1996/359861_06-6_20240715_Hu_nucseq_USZ_EMB015_V3/outs/filtered_feature_bc_matrix                        438
##   /data/mapped/o35986_NovaSeq_240814_NOV1996/359861_07-7_20240715_Hu_nucseq_USZ_EMB016_V1/outs/filtered_feature_bc_matrix                        584
##   /data/mapped/o35986_NovaSeq_240814_NOV1996/359861_08-8_20240715_Hu_nucseq_USZ_EMB016_V2/outs/filtered_feature_bc_matrix                         96
##   /data/mapped/o35986_NovaSeq_240814_NOV1996/359861_18-10_20240730_Hu_nucseq_USZ_EMB016_V3/outs/filtered_feature_bc_matrix                       275
##   /data/mapped/o37256_NovaSeq_241212_NOV2019/372561_08-8_20241204_Hu_nucseq_USZ_HTx004_VX1/outs/filtered_feature_bc_matrix                       995
##   /data/mapped/o37256_NovaSeq_241212_NOV2019/372561_09-9_20241204_Hu_nucseq_USZ_HTx004_VX2/outs/filtered_feature_bc_matrix                       433
##   /data/mapped/o37256_NovaSeq_241212_NOV2019/372561_10-10_20241204_Hu_nucseq_USZ_HTx007_V4/outs/filtered_feature_bc_matrix                       418
##   /data/mapped/o37256_NovaSeq_241212_NOV2019/372561_11-11_20241204_Hu_nucseq_USZ_HTx007_VX1/outs/filtered_feature_bc_matrix                      406
##   /data/mapped/o37256_NovaSeq_241212_NOV2019/372561_12-12_20241204_Hu_nucseq_USZ_HTx007_VX2/outs/filtered_feature_bc_matrix                     1114
##   /data/mapped/o37256_NovaSeq_241212_NOV2019/372561_13-13_20241204_Hu_nucseq_USZ_HTx007_V5/outs/filtered_feature_bc_matrix                        98
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_09-1_20241212_Hu_nucseq_USZ_HTx006_V4/outs/filtered_feature_bc_matrix                           300
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_10-2_20241212_Hu_nucseq_USZ_HTx006_V5/outs/filtered_feature_bc_matrix                           109
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_11-3_20241212_Hu_nucseq_USZ_HTx006_VX1/outs/filtered_feature_bc_matrix                          320
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_12-4_20241212_Hu_nucseq_USZ_HTx006_VX2/outs/filtered_feature_bc_matrix                          354
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_13-5_20241212_Hu_nucseq_USZ_HTx006_VX3/outs/filtered_feature_bc_matrix                           59
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_14-6_20241212_Hu_nucseq_USZ_HTx018_expRV/outs/filtered_feature_bc_matrix                        549
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_15-7_20241212_Hu_nucseq_USZ_HTx018_expLV/outs/filtered_feature_bc_matrix                        431
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_16-8_20241212_Hu_nucseq_USZ_HTx018_V1/outs/filtered_feature_bc_matrix                            60
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_17-9_20241212_Hu_nucseq_USZ_HTx018_V2/outs/filtered_feature_bc_matrix                           369
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_18-10_20241212_Hu_nucseq_USZ_HTx018_V3/outs/filtered_feature_bc_matrix                           42
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_19-11_20241212_Hu_nucseq_USZ_HTx018_V4/outs/filtered_feature_bc_matrix                          413
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_20-12_20241212_Hu_nucseq_USZ_HTx018_VX1/outs/filtered_feature_bc_matrix                         184
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_21-13_20241212_Hu_nucseq_USZ_HTx004_V4/outs/filtered_feature_bc_matrix                         3719
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_22-14_20241212_Hu_nucseq_USZ_HTx004_V5/outs/filtered_feature_bc_matrix                          285
##   /data/mapped/o37746_NovaSeq_250306_X231/377461_2-2_20250212_Hu_nucseq_USZ_HTx004_VX3/outs/filtered_feature_bc_matrix                           456
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_01-1_20250423_Hu_nucseq_USZ_HTx019_V1/outs/filtered_feature_bc_matrix                        684
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_02-2_20250423_Hu_nucseq_USZ_HTx019_V2/outs/filtered_feature_bc_matrix                        868
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_03-3_20250423_Hu_nucseq_USZ_HTx019_V3/outs/filtered_feature_bc_matrix                        445
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_04-4_20250423_Hu_nucseq_USZ_HTx019_VX1/outs/filtered_feature_bc_matrix                       389
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_05-5_20250423_Hu_nucseq_USZ_HTx002_VX2/outs/filtered_feature_bc_matrix                       618
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_06-6_20250423_Hu_nucseq_USZ_HTx002_VX3/outs/filtered_feature_bc_matrix                       710
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_07-7_20250423_Hu_nucseq_USZ_HTx007_VX3/outs/filtered_feature_bc_matrix                       179
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_08-8_20250423_Hu_nucseq_USZ_HTx007_VX4/outs/filtered_feature_bc_matrix                       402
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_09-9_20250423_Hu_nucseq_USZ_HTx008_VX2/outs/filtered_feature_bc_matrix                        61
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_10-10_20250423_Hu_nucseq_USZ_HTx008_VX3/outs/filtered_feature_bc_matrix                      510
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_11-11_20250423_Hu_nucseq_USZ_HTx018_VX2/outs/filtered_feature_bc_matrix                       89
##                                                                                                                                             
##                                                                                                                                              doublet
##   /data/mapped/NovaSeq_20220609_NOV1312_o28576_DataDelivery/o28576_1_08-8_20220525_Hu_nucseq_Graz_8_HH_GEM/outs/filtered_feature_bc_matrix       240
##   /data/mapped/NovaSeq_20220609_NOV1312_o28576_DataDelivery/o28576_1_10-10_20220525_Hu_nucseq_Graz_10_HH_GEM/outs/filtered_feature_bc_matrix     243
##   /data/mapped/NovaSeq_20220609_NOV1312_o28576_DataDelivery/o28576_1_11-11_20220525_Hu_nucseq_Graz_11_HH_GEM/outs/filtered_feature_bc_matrix     279
##   /data/mapped/NovaSeq_20220609_NOV1312_o28576_DataDelivery/o28576_1_12-12_20220525_Hu_nucseq_Graz_12_HH_GEM/outs/filtered_feature_bc_matrix     200
##   /data/mapped/NovaSeq_20220901_NOV1421_o29273_DataDelivery/o292731_1-1_20220818_Hu_nucseq_Graz_9_HH_GEM/outs/filtered_feature_bc_matrix         516
##   /data/mapped/NovaSeq_20220901_NOV1421_o29273_DataDelivery/o292731_2-2_20220818_Hu_nucseq_Graz_13_HH_GEM/outs/filtered_feature_bc_matrix       1226
##   /data/mapped/NovaSeq_20220916_NOV1443_o29478_DataDelivery/o294781_01-1_20220912_Hu_nucseq_Graz_21_HH_GEM/outs/filtered_feature_bc_matrix        60
##   /data/mapped/NovaSeq_20220916_NOV1443_o29478_DataDelivery/o294781_02-2_20220912_Hu_nucseq_Graz_22_HH_GEM/outs/filtered_feature_bc_matrix        87
##   /data/mapped/NovaSeq_20220916_NOV1443_o29478_DataDelivery/o294781_03-3_20220912_Hu_nucseq_Graz_23_HH_GEM/outs/filtered_feature_bc_matrix        35
##   /data/mapped/NovaSeq_20220916_NOV1443_o29478_DataDelivery/o294781_04-4_20220912_Hu_nucseq_Graz_24_HH_GEM/outs/filtered_feature_bc_matrix       113
##   /data/mapped/o33157_NovaSeq_231024_NOV1906/331571_3-5_20231012_Hu_nucseq_USZ_HTx001/outs/filtered_feature_bc_matrix                            227
##   /data/mapped/o33157_NovaSeq_231024_NOV1906/331571_4-6_20231012_Hu_nucseq_USZ_HTx002/outs/filtered_feature_bc_matrix                            151
##   /data/mapped/o33413_NovaSeq_231120_NOV1928/334131_01-1_20231103_Hu_nucseq_USZ_HTx002_RV/outs/filtered_feature_bc_matrix                        323
##   /data/mapped/o33413_NovaSeq_231120_NOV1928/334131_02-2_20231103_Hu_nucseq_USZ_HTx003_LV/outs/filtered_feature_bc_matrix                        309
##   /data/mapped/o33413_NovaSeq_231120_NOV1928/334131_03-3_20231103_Hu_nucseq_USZ_HTx004_RV/outs/filtered_feature_bc_matrix                        317
##   /data/mapped/o33413_NovaSeq_231120_NOV1928/334131_04-4_20231106_Hu_nucseq_USZ_HTx003_RV/outs/filtered_feature_bc_matrix                        279
##   /data/mapped/o33413_NovaSeq_231120_NOV1928/334131_05-5_20231106_Hu_nucseq_USZ_HTx004_LV/outs/filtered_feature_bc_matrix                        461
##   /data/mapped/o33413_NovaSeq_231120_NOV1928/334131_06-6_20231106_Hu_nucseq_USZ_HTx005_RV/outs/filtered_feature_bc_matrix                        159
##   /data/mapped/o33413_NovaSeq_231120_NOV1928/334131_07-7_20231107_Hu_nucseq_USZ_HTx005_LV/outs/filtered_feature_bc_matrix                        107
##   /data/mapped/o33413_NovaSeq_231120_NOV1928/334131_08-8_20231107_Hu_nucseq_USZ_HTx006_RV/outs/filtered_feature_bc_matrix                        158
##   /data/mapped/o33413_NovaSeq_231120_NOV1928/334131_09-9_20231108_Hu_nucseq_USZ_HTx006_LV/outs/filtered_feature_bc_matrix                        242
##   /data/mapped/o33413_NovaSeq_231120_NOV1928/334131_10-10_20231108_Hu_nucseq_USZ_HTx001_RV/outs/filtered_feature_bc_matrix                       107
##   /data/mapped/o33643_NovaSeq_231207_NOV1935/336431_07-7_20231129_Hu_nucseq_USZ_HTx008_RV/outs/filtered_feature_bc_matrix                        454
##   /data/mapped/o33643_NovaSeq_231207_NOV1935/336431_08-8_20231129_Hu_nucseq_USZ_EMB001_V1/outs/filtered_feature_bc_matrix                         18
##   /data/mapped/o33643_NovaSeq_231207_NOV1935/336431_09-9_20231129_Hu_nucseq_USZ_HTx008_LV/outs/filtered_feature_bc_matrix                        276
##   /data/mapped/o33643_NovaSeq_231207_NOV1935/336431_10-10_20231129_Hu_nucseq_USZ_EMB005_V1/outs/filtered_feature_bc_matrix                        49
##   /data/mapped/o33643_NovaSeq_231207_NOV1935/336431_11-11_20231129_Hu_nucseq_USZ_EMB006_V1/outs/filtered_feature_bc_matrix                        14
##   /data/mapped/o33643_NovaSeq_231207_NOV1935/336431_12-12_20231129_Hu_nucseq_USZ_EMB007_V1/outs/filtered_feature_bc_matrix                        68
##   /data/mapped/o33643_NovaSeq_231207_NOV1935/336431_13-13_20231129_Hu_nucseq_USZ_EMB002_V1/outs/filtered_feature_bc_matrix                       100
##   /data/mapped/o33643_NovaSeq_231207_NOV1935/336431_14-14_20231129_Hu_nucseq_USZ_EMB003_V1/outs/filtered_feature_bc_matrix                       109
##   /data/mapped/o33643_NovaSeq_231207_NOV1935/336431_15-15_20231129_Hu_nucseq_USZ_EMB004_V1/outs/filtered_feature_bc_matrix                        93
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_01-7_20240118_Hu_nucseq_USZ_EMB007_V2/outs/filtered_feature_bc_matrix                         90
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_02-8_20240118_Hu_nucseq_USZ_EMB008_V2/outs/filtered_feature_bc_matrix                         19
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_03-9_20240123_Hu_nucseq_USZ_EMB001_V3/outs/filtered_feature_bc_matrix                          5
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_04-10_20240123_Hu_nucseq_USZ_EMB002_V3/outs/filtered_feature_bc_matrix                        30
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_05-11_20240123_Hu_nucseq_USZ_EMB003_V3/outs/filtered_feature_bc_matrix                        11
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_06-12_20240123_Hu_nucseq_USZ_EMB004_V3/outs/filtered_feature_bc_matrix                        44
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_07-13_20240123_Hu_nucseq_USZ_EMB005_V3/outs/filtered_feature_bc_matrix                       106
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_08-14_20240123_Hu_nucseq_USZ_EMB006_V3/outs/filtered_feature_bc_matrix                        23
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_09-15_20240123_Hu_nucseq_USZ_EMB007_V3/outs/filtered_feature_bc_matrix                        26
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_10-16_20240123_Hu_nucseq_USZ_EMB008_V3/outs/filtered_feature_bc_matrix                        11
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_11-17_20240123_Hu_nucseq_USZ_EMB008_V1/outs/filtered_feature_bc_matrix                         6
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_12-18_20240123_Hu_nucseq_USZ_HTx007_RV/outs/filtered_feature_bc_matrix                       198
##   /data/mapped/o34082_NovaSeq_240209_NOV1955/340821_13-19_20240123_Hu_nucseq_USZ_HTx007_LV/outs/filtered_feature_bc_matrix                       158
##   /data/mapped/o34083_NovaSeq_240206_NOV1953/340831_1-1_20240118_Hu_nucseq_USZ_EMB001_V2/outs/filtered_feature_bc_matrix                          56
##   /data/mapped/o34083_NovaSeq_240206_NOV1953/340831_2-2_20240118_Hu_nucseq_USZ_EMB002_V2/outs/filtered_feature_bc_matrix                         180
##   /data/mapped/o34083_NovaSeq_240206_NOV1953/340831_3-3_20240118_Hu_nucseq_USZ_EMB003_V2/outs/filtered_feature_bc_matrix                          14
##   /data/mapped/o34083_NovaSeq_240206_NOV1953/340831_4-4_20240118_Hu_nucseq_USZ_EMB004_V2/outs/filtered_feature_bc_matrix                          20
##   /data/mapped/o34083_NovaSeq_240206_NOV1953/340831_5-5_20240118_Hu_nucseq_USZ_EMB005_V2/outs/filtered_feature_bc_matrix                          25
##   /data/mapped/o34083_NovaSeq_240206_NOV1953/340831_6-6_20240118_Hu_nucseq_USZ_EMB006_V2/outs/filtered_feature_bc_matrix                          33
##   /data/mapped/o34774_NovaSeq_240411_NOV1972/347741_1-1_20240326_Hu_nucseq_USZ_HTx009_RV/outs/filtered_feature_bc_matrix                         282
##   /data/mapped/o34774_NovaSeq_240411_NOV1972/347741_2-2_20240326_Hu_nucseq_USZ_HTx009_LV/outs/filtered_feature_bc_matrix                         602
##   /data/mapped/o34774_NovaSeq_240411_NOV1972/347741_4-4_20240326_Hu_nucseq_USZ_HTx010_RV/outs/filtered_feature_bc_matrix                         122
##   /data/mapped/o34774_NovaSeq_240411_NOV1972/347741_5-5_20240326_Hu_nucseq_USZ_HTx010_LV/outs/filtered_feature_bc_matrix                         193
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_01-1_20240515_Hu_nucseq_USZ_HTx011_RV/outs/filtered_feature_bc_matrix                        111
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_02-2_20240515_Hu_nucseq_USZ_HTx011_LV/outs/filtered_feature_bc_matrix                         59
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_03-3_20240515_Hu_nucseq_USZ_EMB011_V1/outs/filtered_feature_bc_matrix                         58
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_04-4_20240515_Hu_nucseq_USZ_EMB011_V2/outs/filtered_feature_bc_matrix                          9
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_05-5_20240515_Hu_nucseq_USZ_HTx012_RV/outs/filtered_feature_bc_matrix                         66
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_06-6_20240515_Hu_nucseq_USZ_HTx012_LV/outs/filtered_feature_bc_matrix                        199
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_07-7_20240515_Hu_nucseq_USZ_EMB012_V1/outs/filtered_feature_bc_matrix                         30
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_08-8_20240515_Hu_nucseq_USZ_EMB010_V3/outs/filtered_feature_bc_matrix                         27
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_09-9_20240515_Hu_nucseq_USZ_HTx013_RV/outs/filtered_feature_bc_matrix                         95
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_10-10_20240515_Hu_nucseq_USZ_HTx013_LV/outs/filtered_feature_bc_matrix                       159
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_11-11_20240515_Hu_nucseq_USZ_EMB010_V1_1/outs/filtered_feature_bc_matrix                      45
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_12-12_20240515_Hu_nucseq_USZ_EMB010_V1_2/outs/filtered_feature_bc_matrix                      36
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_13-13_20240524_Hu_nucseq_USZ_HTx015_RV/outs/filtered_feature_bc_matrix                       122
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_14-14_20240524_Hu_nucseq_USZ_EMB011_V3/outs/filtered_feature_bc_matrix                        44
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_15-15_20240524_Hu_nucseq_USZ_EMB012_V2/outs/filtered_feature_bc_matrix                       158
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_17-17_20240524_Hu_nucseq_USZ_HTx015_LV/outs/filtered_feature_bc_matrix                        63
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_18-18_20240524_Hu_nucseq_USZ_EMB013_V1/outs/filtered_feature_bc_matrix                        21
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_19-19_20240524_Hu_nucseq_USZ_EMB013_V2/outs/filtered_feature_bc_matrix                        14
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_20-20_20240524_Hu_nucseq_USZ_EMB013_V3/outs/filtered_feature_bc_matrix                        12
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_21-21_20240524_Hu_nucseq_USZ_HTx014_RV/outs/filtered_feature_bc_matrix                       154
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_22-22_20240524_Hu_nucseq_USZ_HTx014_LV/outs/filtered_feature_bc_matrix                       172
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_23-23_20240524_Hu_nucseq_USZ_EMB014_V1/outs/filtered_feature_bc_matrix                        45
##   /data/mapped/o35392_NovaSeq_240624_NOV1989/353921_24-24_20240524_Hu_nucseq_USZ_EMB014_V2/outs/filtered_feature_bc_matrix                        20
##   /data/mapped/o35986_NovaSeq_240814_NOV1996/359861_01-1_20240715_Hu_nucseq_USZ_HTx016_LV/outs/filtered_feature_bc_matrix                        618
##   /data/mapped/o35986_NovaSeq_240814_NOV1996/359861_02-2_20240715_Hu_nucseq_USZ_HTx016_RV/outs/filtered_feature_bc_matrix                        825
##   /data/mapped/o35986_NovaSeq_240814_NOV1996/359861_03-3_20240715_Hu_nucseq_USZ_EMB014_V3/outs/filtered_feature_bc_matrix                         18
##   /data/mapped/o35986_NovaSeq_240814_NOV1996/359861_04-4_20240715_Hu_nucseq_USZ_EMB015_V1/outs/filtered_feature_bc_matrix                          5
##   /data/mapped/o35986_NovaSeq_240814_NOV1996/359861_05-5_20240715_Hu_nucseq_USZ_EMB015_V2/outs/filtered_feature_bc_matrix                         16
##   /data/mapped/o35986_NovaSeq_240814_NOV1996/359861_06-6_20240715_Hu_nucseq_USZ_EMB015_V3/outs/filtered_feature_bc_matrix                         39
##   /data/mapped/o35986_NovaSeq_240814_NOV1996/359861_07-7_20240715_Hu_nucseq_USZ_EMB016_V1/outs/filtered_feature_bc_matrix                         23
##   /data/mapped/o35986_NovaSeq_240814_NOV1996/359861_08-8_20240715_Hu_nucseq_USZ_EMB016_V2/outs/filtered_feature_bc_matrix                         11
##   /data/mapped/o35986_NovaSeq_240814_NOV1996/359861_18-10_20240730_Hu_nucseq_USZ_EMB016_V3/outs/filtered_feature_bc_matrix                        20
##   /data/mapped/o37256_NovaSeq_241212_NOV2019/372561_08-8_20241204_Hu_nucseq_USZ_HTx004_VX1/outs/filtered_feature_bc_matrix                        49
##   /data/mapped/o37256_NovaSeq_241212_NOV2019/372561_09-9_20241204_Hu_nucseq_USZ_HTx004_VX2/outs/filtered_feature_bc_matrix                        20
##   /data/mapped/o37256_NovaSeq_241212_NOV2019/372561_10-10_20241204_Hu_nucseq_USZ_HTx007_V4/outs/filtered_feature_bc_matrix                        13
##   /data/mapped/o37256_NovaSeq_241212_NOV2019/372561_11-11_20241204_Hu_nucseq_USZ_HTx007_VX1/outs/filtered_feature_bc_matrix                       26
##   /data/mapped/o37256_NovaSeq_241212_NOV2019/372561_12-12_20241204_Hu_nucseq_USZ_HTx007_VX2/outs/filtered_feature_bc_matrix                       52
##   /data/mapped/o37256_NovaSeq_241212_NOV2019/372561_13-13_20241204_Hu_nucseq_USZ_HTx007_V5/outs/filtered_feature_bc_matrix                         6
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_09-1_20241212_Hu_nucseq_USZ_HTx006_V4/outs/filtered_feature_bc_matrix                            21
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_10-2_20241212_Hu_nucseq_USZ_HTx006_V5/outs/filtered_feature_bc_matrix                             5
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_11-3_20241212_Hu_nucseq_USZ_HTx006_VX1/outs/filtered_feature_bc_matrix                           20
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_12-4_20241212_Hu_nucseq_USZ_HTx006_VX2/outs/filtered_feature_bc_matrix                           12
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_13-5_20241212_Hu_nucseq_USZ_HTx006_VX3/outs/filtered_feature_bc_matrix                            3
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_14-6_20241212_Hu_nucseq_USZ_HTx018_expRV/outs/filtered_feature_bc_matrix                         21
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_15-7_20241212_Hu_nucseq_USZ_HTx018_expLV/outs/filtered_feature_bc_matrix                         14
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_16-8_20241212_Hu_nucseq_USZ_HTx018_V1/outs/filtered_feature_bc_matrix                             0
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_17-9_20241212_Hu_nucseq_USZ_HTx018_V2/outs/filtered_feature_bc_matrix                            13
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_18-10_20241212_Hu_nucseq_USZ_HTx018_V3/outs/filtered_feature_bc_matrix                            0
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_19-11_20241212_Hu_nucseq_USZ_HTx018_V4/outs/filtered_feature_bc_matrix                           17
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_20-12_20241212_Hu_nucseq_USZ_HTx018_VX1/outs/filtered_feature_bc_matrix                          15
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_21-13_20241212_Hu_nucseq_USZ_HTx004_V4/outs/filtered_feature_bc_matrix                          307
##   /data/mapped/o37329_NovaSeq_250121_X204/373291_22-14_20241212_Hu_nucseq_USZ_HTx004_V5/outs/filtered_feature_bc_matrix                            9
##   /data/mapped/o37746_NovaSeq_250306_X231/377461_2-2_20250212_Hu_nucseq_USZ_HTx004_VX3/outs/filtered_feature_bc_matrix                            24
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_01-1_20250423_Hu_nucseq_USZ_HTx019_V1/outs/filtered_feature_bc_matrix                         22
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_02-2_20250423_Hu_nucseq_USZ_HTx019_V2/outs/filtered_feature_bc_matrix                         39
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_03-3_20250423_Hu_nucseq_USZ_HTx019_V3/outs/filtered_feature_bc_matrix                         27
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_04-4_20250423_Hu_nucseq_USZ_HTx019_VX1/outs/filtered_feature_bc_matrix                        19
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_05-5_20250423_Hu_nucseq_USZ_HTx002_VX2/outs/filtered_feature_bc_matrix                        24
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_06-6_20250423_Hu_nucseq_USZ_HTx002_VX3/outs/filtered_feature_bc_matrix                        27
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_07-7_20250423_Hu_nucseq_USZ_HTx007_VX3/outs/filtered_feature_bc_matrix                         7
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_08-8_20250423_Hu_nucseq_USZ_HTx007_VX4/outs/filtered_feature_bc_matrix                         0
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_09-9_20250423_Hu_nucseq_USZ_HTx008_VX2/outs/filtered_feature_bc_matrix                         3
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_10-10_20250423_Hu_nucseq_USZ_HTx008_VX3/outs/filtered_feature_bc_matrix                       32
##   /data/mapped/o38409_NovaSeq_250502_NOV2038/384091_11-11_20250423_Hu_nucseq_USZ_HTx018_VX2/outs/filtered_feature_bc_matrix                        4
seuratM$clusterName <- "clusterName"
seuratM$clusterName[which(seuratM$RNA_snn_res.0.4 %in% "0" )] <- "Fb1"
seuratM$clusterName[which(seuratM$RNA_snn_res.0.4 %in% "1" )] <- "PerivFb1"
seuratM$clusterName[which(seuratM$RNA_snn_res.0.4 %in% "2" )] <- "Mph2"
seuratM$clusterName[which(seuratM$RNA_snn_res.0.4 %in% "3" )] <- "BEC1"
seuratM$clusterName[which(seuratM$RNA_snn_res.0.4 %in% "4" )] <- "Fb2"
seuratM$clusterName[which(seuratM$RNA_snn_res.0.4 %in% "5" )] <- "CM"
seuratM$clusterName[which(seuratM$RNA_snn_res.0.4 %in% "6" )] <- "Tcell1"
seuratM$clusterName[which(seuratM$RNA_snn_res.0.4 %in% "7" )] <- "BEC2"
seuratM$clusterName[which(seuratM$RNA_snn_res.0.4 %in% "8" )] <- "VSMC"
seuratM$clusterName[which(seuratM$RNA_snn_res.0.4 %in% "9" )] <- "Mph1"
seuratM$clusterName[which(seuratM$RNA_snn_res.0.4 %in% "10" )] <- "BEC3"
seuratM$clusterName[which(seuratM$RNA_snn_res.0.4 %in% "11" )] <- "NC"
seuratM$clusterName[which(seuratM$RNA_snn_res.0.4 %in% "12" )] <- "BaroRec"
seuratM$clusterName[which(seuratM$RNA_snn_res.0.4 %in% "13" )] <- "Bcell"
seuratM$clusterName[which(seuratM$RNA_snn_res.0.4 %in% "14" )] <- "Fb3"
seuratM$clusterName[which(seuratM$RNA_snn_res.0.4 %in% "15" )] <- "Tcell2"
seuratM$clusterName[which(seuratM$RNA_snn_res.0.4 %in% "16" )] <- "LEC"
seuratM$clusterName[which(seuratM$RNA_snn_res.0.4 %in% "17" )] <- "PerivFb2"
seuratM$clusterName[which(seuratM$RNA_snn_res.0.4 %in% "18" )] <- "Adipoc"
pdf(file.path(output_path,"Cluster Doublet Summary.pdf"), height = 8, width = 10)
doublets_summary <- seuratM@meta.data%>%
  group_by(clusterName, scDblFinder.class)%>%
  summarise(total_count=n(), .groups="drop")%>%as.data.frame()%>%ungroup()%>%
              group_by(clusterName)%>%
              mutate(countT=sum(total_count))%>%
              group_by(scDblFinder.class, .add=TRUE)%>%
              mutate(percent=paste0(round(100*total_count/countT,2),"%"))%>%
              dplyr::select(-countT)
dev.off
## function (which = dev.cur()) 
## {
##     if (which == 1) 
##         stop("cannot shut down device 1 (the null device)")
##     .External(C_devoff, as.integer(which))
##     dev.cur()
## }
## <bytecode: 0x12555b1e8>
## <environment: namespace:grDevices>
doublets_summary
## # A tibble: 38 × 4
## # Groups:   clusterName, scDblFinder.class [38]
##    clusterName scDblFinder.class total_count percent
##    <chr>       <fct>                   <int> <chr>  
##  1 Adipoc      singlet                   679 84.88% 
##  2 Adipoc      doublet                   121 15.12% 
##  3 BEC1        singlet                 24736 98.6%  
##  4 BEC1        doublet                   352 1.4%   
##  5 BEC2        singlet                  7017 98.82% 
##  6 BEC2        doublet                    84 1.18%  
##  7 BEC3        singlet                  3450 88.83% 
##  8 BEC3        doublet                   434 11.17% 
##  9 BaroRec     singlet                  1305 53.33% 
## 10 BaroRec     doublet                  1142 46.67% 
## # ℹ 28 more rows

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

VlnPlot(seuratM, group.by = "Sample", split.by = "scDblFinder.class",
        features = c("nFeature_RNA", "nCount_RNA", "subsets_Mt_percent"),
        ncol=3, pt.size=0, raster = FALSE) + theme(legend.position = "right")
## The default behaviour of split.by has changed.
## Separate violin plots are now plotted side-by-side.
## To restore the old behaviour of a single split violin,
## set split.plot = TRUE.
##       
## This message will be shown once per session.
## Warning: The `slot` argument of `FetchData()` is deprecated as of SeuratObject 5.0.0.
## ℹ Please use the `layer` argument instead.
## ℹ The deprecated feature was likely used in the Seurat package.
##   Please report the issue at <https://github.com/satijalab/seurat/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
## ℹ Please use tidy evaluation idioms with `aes()`.
## ℹ See also `vignette("ggplot2-in-packages")` for more information.
## ℹ The deprecated feature was likely used in the Seurat package.
##   Please report the issue at <https://github.com/satijalab/seurat/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

VlnPlot(seuratM, group.by = "clusterName", split.by = "scDblFinder.class",
        features = c("nFeature_RNA", "nCount_RNA", "subsets_Mt_percent"),
        ncol=3, pt.size=0) + theme(legend.position = "right")
## Rasterizing points since number of points exceeds 100,000.
## To disable this behavior set `raster=FALSE`
## Rasterizing points since number of points exceeds 100,000.
## To disable this behavior set `raster=FALSE`
## Rasterizing points since number of points exceeds 100,000.
## To disable this behavior set `raster=FALSE`

Idents(seuratM) <- seuratM$scDblFinder.class
colsDblFinder <- c("lightblue", "red")
DimPlot(seuratM, reduction = "umap", pt.size= 0.1, cols= colsDblFinder, raster = FALSE)

Idents(seuratM) <-seuratM$scDblFinder.score>0.9
DimPlot(seuratM, reduction = "umap", pt.size= 0.1, cols= colsDblFinder, raster=FALSE) +
  ggtitle("DblFinder Score >0.9") +
  theme(plot.title = element_text(hjust = 0.5))

#Without Cluster Annotation
Idents(seuratM) <- seuratM$RNA_snn_res.0.4
DimPlot(seuratM, reduction = "umap", pt.size = 0.1, raster = FALSE) 

#With Cluster Annotation
colclusterName <- c("#67001f", "#f4a582","#D53E4F", "#B45B5C","#003c30","#01665e","#66C2A5", "#BEAEF8","#BEAED4", "#c7eae5", "#B09C85", "#4e5a4c","#393A3F","pink","#4588CA","#3299CA","#FCC80B","#FEE60B","#628395")
names(colclusterName) <- c("CM","Fb1","Fb2","Fb3","PerivFb1","PerivFb2","VSMC","BEC1","BEC2","BEC3","LEC","NC","BaroRec","Adipoc","Mph1","Mph2","Tcell1","Tcell2","Bcell")
Idents(seuratM) <- seuratM$clusterName
DimPlot(seuratM, reduction = "umap", pt.size = 0.1, cols = colclusterName, raster = FALSE)

Small clusters are doublets, with high Dbl score >0.9 matching doublet classification –> Doublets will be removed

seuratMS <- subset(seuratM, subset = scDblFinder.class == "singlet")

#Rerun Seurat
seuratMS <- NormalizeData (object = seuratMS)
## Normalizing layer: counts
seuratMS <- FindVariableFeatures(object = seuratMS)
## Finding variable features for layer counts
seuratMS <- ScaleData(object = seuratMS, verbose = TRUE)
## Centering and scaling data matrix
## Warning: Different features in new layer data than already exists for
## scale.data
seuratMS <- RunPCA(object=seuratMS, npcs = 30, verbose = FALSE)
ElbowPlot(seuratMS) #Ranks PC based on the percentage of variance explained by each one. At flattening curve, the majority of true signal is captured by this number of PCs. Including too many PCs usually does not affect much the result, while including too few PCs can affect the results very much

seuratMS <- RunTSNE(object=seuratMS, reduction="pca", dims = 1:20)
seuratMS <- RunUMAP(object=seuratMS, reduction="pca", dims = 1:20)
## Warning: The default method for RunUMAP has changed from calling Python UMAP via reticulate to the R-native UWOT using the cosine metric
## To use Python UMAP via reticulate, set umap.method to 'umap-learn' and metric to 'correlation'
## This message will be shown once per session
## 10:09:29 UMAP embedding parameters a = 0.9922 b = 1.112
## 10:09:29 Read 186338 rows and found 20 numeric columns
## 10:09:29 Using Annoy for neighbor search, n_neighbors = 30
## 10:09:29 Building Annoy index with metric = cosine, n_trees = 50
## 0%   10   20   30   40   50   60   70   80   90   100%
## [----|----|----|----|----|----|----|----|----|----|
## **************************************************|
## 10:09:38 Writing NN index file to temp file /var/folders/gw/vzkwyjjn43q4n0p7gm_zyncm0000gn/T//RtmpOawzL7/file8fa9595f5361
## 10:09:38 Searching Annoy index using 1 thread, search_k = 3000
## 10:10:14 Annoy recall = 100%
## 10:10:15 Commencing smooth kNN distance calibration using 1 thread with target n_neighbors = 30
## 10:10:17 Initializing from normalized Laplacian + noise (using RSpectra)
## 10:10:33 Commencing optimization for 200 epochs, with 8595948 positive edges
## 10:10:33 Using rng type: pcg
## 10:11:09 Optimization finished
seuratMS <- FindNeighbors(object = seuratMS, reduction = "pca", dims= 1:20)
## Computing nearest neighbor graph
## Computing SNN
res <- c(0.25, 0.6, 0.8, 0.4) #If other resolutions are warranted this needs to be changed
for (i in 1:length(res)) {
  seuratMS <- FindClusters(object = seuratMS, resolution = res[i], random.seed = 1234)
}
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
## 
## Number of nodes: 186338
## Number of edges: 5711761
## 
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.9558
## Number of communities: 14
## Elapsed time: 58 seconds
## 1 singletons identified. 13 final clusters.
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
## 
## Number of nodes: 186338
## Number of edges: 5711761
## 
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.9207
## Number of communities: 28
## Elapsed time: 59 seconds
## 1 singletons identified. 27 final clusters.
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
## 
## Number of nodes: 186338
## Number of edges: 5711761
## 
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.9085
## Number of communities: 29
## Elapsed time: 67 seconds
## 1 singletons identified. 28 final clusters.
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
## 
## Number of nodes: 186338
## Number of edges: 5711761
## 
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.9380
## Number of communities: 21
## Elapsed time: 59 seconds
## 1 singletons identified. 20 final clusters.
sce2 <- as.SingleCellExperiment(seuratMS)
#Set seed to ensure reproducibility
seed <- 1234
bp <- MulticoreParam(3, RNGseed=1234)
sce2 <- scDblFinder(sce2, samples = "Sample", BPPARAM = bp)
table(sce2$scDblFinder.class)
## 
## singlet doublet 
##  180126    6212
Idents(seuratMS) <- seuratMS$RNA_snn_res.0.4
DimPlot(seuratMS, reduction = "umap", pt.size = 0.1, raster=FALSE) 

Idents(seuratMS) <- seuratMS$RNA_snn_res.0.25
DimPlot(seuratMS, reduction = "umap", pt.size = 0.1, raster=FALSE) 

saveRDS(seuratMS, file="/Users/immbio/Desktop/HumanHeartCarTrans2/data/Human_heart_allmerged_seurat_Singlets.rds")

#Quality Control

seuratMS$patient <- "pat_nr"
seuratMS$patient[grepl("HTx001|EMB001", seuratMS$dataset)] <- "CarTransPat01"
seuratMS$patient[grepl("HTx002|EMB002", seuratMS$dataset)] <- "CarTransPat02"
seuratMS$patient[grepl("HTx003|EMB003", seuratMS$dataset)] <- "CarTransPat03"
seuratMS$patient[grepl("HTx004|EMB004", seuratMS$dataset)] <- "CarTransPat04"
seuratMS$patient[grepl("HTx005|EMB005", seuratMS$dataset)] <- "CarTransPat05"
seuratMS$patient[grepl("HTx006|EMB006", seuratMS$dataset)] <- "CarTransPat06"
seuratMS$patient[grepl("HTx007|EMB007", seuratMS$dataset)] <- "CarTransPat07"
seuratMS$patient[grepl("HTx008|EMB008", seuratMS$dataset)] <- "CarTransPat08"
seuratMS$patient[grepl("HTx009|EMB009", seuratMS$dataset)] <- "CarTransPat09"
seuratMS$patient[grepl("HTx010|EMB010", seuratMS$dataset)] <- "CarTransPat10"
seuratMS$patient[grepl("HTx011|EMB011", seuratMS$dataset)] <- "CarTransPat11"
seuratMS$patient[grepl("HTx012|EMB012", seuratMS$dataset)] <- "CarTransPat12"
seuratMS$patient[grepl("HTx013|EMB013", seuratMS$dataset)] <- "CarTransPat13"
seuratMS$patient[grepl("HTx014|EMB014", seuratMS$dataset)] <- "CarTransPat14"
seuratMS$patient[grepl("HTx015|EMB015", seuratMS$dataset)] <- "CarTransPat15"
seuratMS$patient[grepl("HTx016|EMB016", seuratMS$dataset)] <- "CarTransPat16"
seuratMS$patient[grepl("HTx018|EMB018", seuratMS$dataset)] <- "CarTransPat18"
seuratMS$patient[grepl("HTx019|EMB019", seuratMS$dataset)] <- "CarTransPat19"
seuratMS$patient[grepl("HTx024|EMB024", seuratMS$dataset)] <- "CarTransPat24"

seuratMS$patient[which(seuratMS$dataset == "o28576_1_08-8_20220525_Hu_nucseq_Graz_8_HH_GEM")] <- "DH01"
seuratMS$patient[which(seuratMS$dataset == "o28576_1_10-10_20220525_Hu_nucseq_Graz_10_HH_GEM")] <- "DH02"
seuratMS$patient[which(seuratMS$dataset == "o28576_1_11-11_20220525_Hu_nucseq_Graz_11_HH_GEM")] <- "DH03"
seuratMS$patient[which(seuratMS$dataset == "o28576_1_12-12_20220525_Hu_nucseq_Graz_12_HH_GEM")] <- "DH04"
seuratMS$patient[which(seuratMS$dataset =="o292731_1-1_20220818_Hu_nucseq_Graz_9_HH_GEM")] <- "DH05"
seuratMS$patient[which(seuratMS$dataset =="o292731_2-2_20220818_Hu_nucseq_Graz_13_HH_GEM")] <- "DH06"
seuratMS$patient[which(seuratMS$dataset == "o294781_01-1_20220912_Hu_nucseq_Graz_21_HH_GEM")] <- "DH07"
seuratMS$patient[which(seuratMS$dataset == "o294781_02-2_20220912_Hu_nucseq_Graz_22_HH_GEM")] <- "DH08"
seuratMS$patient[which(seuratMS$dataset == "o294781_03-3_20220912_Hu_nucseq_Graz_23_HH_GEM")] <- "DH09"
seuratMS$patient[which(seuratMS$dataset == "o294781_04-4_20220912_Hu_nucseq_Graz_24_HH_GEM")] <- "DH10"

#Disease Condition
seuratMS$diseaseCond <- "diseaseCond"
seuratMS$diseaseCond[grepl("V1", seuratMS$dataset)] <- "visit1"
seuratMS$diseaseCond[grepl("V2|353921_12-12_20240515_Hu_nucseq_USZ_EMB010_V1_2", seuratMS$dataset)] <- "visit2"
seuratMS$diseaseCond[grepl("V3", seuratMS$dataset)] <- "visit3"
seuratMS$diseaseCond[grepl("V4", seuratMS$dataset)] <- "visit4"
seuratMS$diseaseCond[grepl("V5", seuratMS$dataset)] <- "visit5"
seuratMS$diseaseCond[grepl("VX1", seuratMS$dataset)] <- "visitX1"
seuratMS$diseaseCond[grepl("VX2", seuratMS$dataset)] <- "visitX2"
seuratMS$diseaseCond[grepl("VX3", seuratMS$dataset)] <- "visitX3"
seuratMS$diseaseCond[grepl("VX4", seuratMS$dataset)] <- "visitX4"
seuratMS$diseaseCond[grepl("HH", seuratMS$dataset)] <- "donorheart"
seuratMS$diseaseCond[grepl("RV|LV|expLV|expRV|331571_3-5_20231012_Hu_nucseq_USZ_HTx001|331571_4-6_20231012_Hu_nucseq_USZ_HTx002", seuratMS$dataset)] <- "explant"

orddiseaseCond <- c("donorheart","visit1", "visit2" ,"visit3", "visit4", "visit5", "visitX1", "visitX2", "visitX3", "visitX4", "explant")

#Combined slots
seuratMS$patient_diseaseCond <- paste0(seuratMS$patient, '_', seuratMS$diseaseCond)
table(seuratMS$patient_diseaseCond)
## 
## CarTransPat01_explant  CarTransPat01_visit1  CarTransPat01_visit2 
##                  5195                   491                  1128 
##  CarTransPat01_visit3 CarTransPat02_explant  CarTransPat02_visit1 
##                    66                  6593                  1841 
##  CarTransPat02_visit2  CarTransPat02_visit3 CarTransPat02_visitX2 
##                  2796                   338                   618 
## CarTransPat02_visitX3 CarTransPat03_explant  CarTransPat03_visit1 
##                   710                  7549                  1878 
##  CarTransPat03_visit2  CarTransPat03_visit3 CarTransPat04_explant 
##                   404                   164                  8733 
##  CarTransPat04_visit1  CarTransPat04_visit2  CarTransPat04_visit3 
##                  1595                   618                   813 
##  CarTransPat04_visit4  CarTransPat04_visit5 CarTransPat04_visitX1 
##                  3719                   285                   995 
## CarTransPat04_visitX2 CarTransPat04_visitX3 CarTransPat05_explant 
##                   433                   456                  4545 
##  CarTransPat05_visit1  CarTransPat05_visit2  CarTransPat05_visit3 
##                  1083                   694                  1951 
## CarTransPat06_explant  CarTransPat06_visit1  CarTransPat06_visit2 
##                  5698                   479                   830 
##  CarTransPat06_visit3  CarTransPat06_visit4  CarTransPat06_visit5 
##                   235                   300                   109 
## CarTransPat06_visitX1 CarTransPat06_visitX2 CarTransPat06_visitX3 
##                   320                   354                    59 
## CarTransPat07_explant  CarTransPat07_visit1  CarTransPat07_visit2 
##                  5025                  1456                  1748 
##  CarTransPat07_visit3  CarTransPat07_visit4  CarTransPat07_visit5 
##                   377                   418                    98 
## CarTransPat07_visitX1 CarTransPat07_visitX2 CarTransPat07_visitX3 
##                   406                  1114                   179 
## CarTransPat07_visitX4 CarTransPat08_explant  CarTransPat08_visit1 
##                   402                  8501                    49 
##  CarTransPat08_visit2  CarTransPat08_visit3 CarTransPat08_visitX2 
##                   570                   182                    61 
## CarTransPat08_visitX3 CarTransPat09_explant CarTransPat10_explant 
##                   510                  9658                  5059 
##  CarTransPat10_visit1  CarTransPat10_visit2  CarTransPat10_visit3 
##                  1073                   256                   716 
## CarTransPat11_explant  CarTransPat11_visit1  CarTransPat11_visit2 
##                  3217                   799                   236 
##  CarTransPat11_visit3 CarTransPat12_explant  CarTransPat12_visit1 
##                   628                  4266                   796 
##  CarTransPat12_visit2 CarTransPat13_explant  CarTransPat13_visit1 
##                  2582                  4328                   520 
##  CarTransPat13_visit2  CarTransPat13_visit3 CarTransPat14_explant 
##                   125                   389                  4818 
##  CarTransPat14_visit1  CarTransPat14_visit2  CarTransPat14_visit3 
##                  1061                   340                   419 
## CarTransPat15_explant  CarTransPat15_visit1  CarTransPat15_visit2 
##                  3533                   173                   200 
##  CarTransPat15_visit3 CarTransPat16_explant  CarTransPat16_visit1 
##                   438                 12435                   584 
##  CarTransPat16_visit2  CarTransPat16_visit3 CarTransPat18_explant 
##                    96                   275                   980 
##  CarTransPat18_visit1  CarTransPat18_visit2  CarTransPat18_visit3 
##                    60                   369                    42 
##  CarTransPat18_visit4 CarTransPat18_visitX1 CarTransPat18_visitX2 
##                   413                   184                    89 
##  CarTransPat19_visit1  CarTransPat19_visit2  CarTransPat19_visit3 
##                   684                   868                   445 
## CarTransPat19_visitX1       DH01_donorheart       DH02_donorheart 
##                   389                  3765                  3679 
##       DH03_donorheart       DH04_donorheart       DH05_donorheart 
##                  3986                  3653                  5918 
##       DH06_donorheart       DH07_donorheart       DH08_donorheart 
##                 10342                  1405                  1977 
##       DH09_donorheart       DH10_donorheart 
##                   831                  2068
table(seuratMS$orig.ident)
## 
##        
## 186338

seuratM 199757 Nuclei seuratMS 186338 Nuclei

#Marker Genes Calculation

seuratMS$clusterName <- "clusterName"
seuratMS$clusterName[which(seuratMS$RNA_snn_res.0.4 %in% "0" )] <- "0"
seuratMS$clusterName[which(seuratMS$RNA_snn_res.0.4 %in% "1" )] <- "1"
seuratMS$clusterName[which(seuratMS$RNA_snn_res.0.4 %in% "2" )] <- "2"
seuratMS$clusterName[which(seuratMS$RNA_snn_res.0.4 %in% "3" )] <- "3"
seuratMS$clusterName[which(seuratMS$RNA_snn_res.0.4 %in% "4" )] <- "4"
seuratMS$clusterName[which(seuratMS$RNA_snn_res.0.4 %in% "5" )] <- "5"
seuratMS$clusterName[which(seuratMS$RNA_snn_res.0.4 %in% "6" )] <- "6"
seuratMS$clusterName[which(seuratMS$RNA_snn_res.0.4 %in% "7" )] <- "7"
seuratMS$clusterName[which(seuratMS$RNA_snn_res.0.4 %in% "8" )] <- "8"
seuratMS$clusterName[which(seuratMS$RNA_snn_res.0.4 %in% "9" )] <- "9"
seuratMS$clusterName[which(seuratMS$RNA_snn_res.0.4 %in% "10" )] <- "10"
seuratMS$clusterName[which(seuratMS$RNA_snn_res.0.4 %in% "11" )] <- "11"
seuratMS$clusterName[which(seuratMS$RNA_snn_res.0.4 %in% "12" )] <- "12"
seuratMS$clusterName[which(seuratMS$RNA_snn_res.0.4 %in% "13" )] <- "13"
seuratMS$clusterName[which(seuratMS$RNA_snn_res.0.4 %in% "14" )] <- "14"
seuratMS$clusterName[which(seuratMS$RNA_snn_res.0.4 %in% "15" )] <- "15"
seuratMS$clusterName[which(seuratMS$RNA_snn_res.0.4 %in% "16" )] <- "16"
seuratMS$clusterName[which(seuratMS$RNA_snn_res.0.4 %in% "17" )] <- "17"
seuratMS$clusterName[which(seuratMS$RNA_snn_res.0.4 %in% "18" )] <- "18"
seuratMS$clusterName[which(seuratMS$RNA_snn_res.0.4 %in% "19" )] <- "19"

colclusterName <- c("#67001f", "#f4a582","#D53E4F", "#B45B5C","#003c30","#01665e","#66C2A5", "#BEAEF8","#BEAED4", "#c7eae5", "#B09C85", "#4e5a4c","#393A3F","pink","#4588CA","#3299CA","#FCC80B","#FEE60B","#628395", "grey")
names(colclusterName) <- c("0", "1", "2", "3","4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19")

coldiseaseCond <- c("#dfc27d","#BE3144","#f4a582","#B45B5C","#8c510a","#202547","#355C7D","#779d8d", "#01665e", "#3288BD", "#BEAED4") 
names(coldiseaseCond) <- c("donorheart", "explant", "visit1", "visit2", "visit3", "visit4", "visit5", "visitX1", "visitX2", "visitX3", "visitX4")

genes <- data.frame(gene=rownames(seuratM)) %>% 
  mutate(geneID=gsub("^.*\\.", "", gene))

selGenes <- data.frame(geneID=rev(c("TTN", "MYBPC3", "RYR2", "NEBL", "TNNT2", "CMYA5", "COL6A3", "DCN",  "FBN1", "C7", "PDGFRA", "CDH19", "PDGFRB","ITGA7","RGS5", "NOTCH3", "MYH11", "ACTA2","PECAM1", "VWF", "EGFL7", "POSTN", "ITGA10", "CDH11","CCL21", "PROX1", "FLT4", "NRXN1", "ANK3", "PTPRZ1", "ACACB", "PLIN1", "GPAM", "CD163", "MRC1", "SIGLEC1", "STAB1", "CSF1R", "MERTK", "IL7R", "PTPRC", "CD2", "PAX5", "IGHM", "IGKC", "MS4A1"))) %>% left_join(., genes, by="geneID") %>% filter(gene != "ENSG00000232995.RGS5") 
##cluster marker
Idents(seuratMS) <- seuratMS$RNA_snn_res.0.4
markerGenes <- FindAllMarkers(seuratMS, only.pos=T) %>% 
  dplyr::filter(p_val_adj < 0.01)
#save table
write.table(markerGenes, 
            file= "/Users/immbio/Desktop/HumanHeartCarTrans2/analysis/SingletsmarkerGenesRNA_snn_res.0.4",
            sep="\t",
            quote=F,
            row.names=F,
            col.names=T)

average heatmap marker genes

Idents(seuratMS) <- seuratMS$clusterName
levels(seuratMS)
##  [1] "2"  "0"  "6"  "3"  "1"  "11" "4"  "13" "8"  "17" "7"  "9"  "5"  "12" "19"
## [16] "15" "14" "16" "18" "10"
seuratS <- seuratMS

avgHeatmap <- function(seuratS, selGenes, colVecIdent, colVecCond=NULL,
                       ordVec=NULL, gapVecR=NULL, gapVecC=NULL,cc=FALSE,
                       cr=FALSE, condCol=FALSE){
  selGenes <- selGenes$gene
  ## assay data
  clusterAssigned <- as.data.frame(Idents(seuratS)) %>%
  dplyr::mutate(cell=rownames(.))
  colnames(clusterAssigned)[1] <- "ident"
  seuratSDat <- GetAssayData(seuratS)
  
  ## genes of interest
  genes <- data.frame(gene=rownames(seuratS)) %>% 
    mutate(geneID=gsub("^.*\\.", "", gene)) %>% filter(geneID %in% selGenes)

  ## matrix with averaged cnts per ident
  logNormExpres <- as.data.frame(t(as.matrix(
    seuratSDat[which(rownames(seuratSDat) %in% genes$gene),])))
  logNormExpres <- logNormExpres %>% dplyr::mutate(cell=rownames(.)) %>%
    dplyr::left_join(.,clusterAssigned, by=c("cell")) %>%
    dplyr::select(-cell) %>% dplyr::group_by(ident) %>%
    dplyr::summarise_all(mean)
  logNormExpresMa <- logNormExpres %>% dplyr::select(-ident) %>% as.matrix()
  rownames(logNormExpresMa) <- logNormExpres$ident
  logNormExpresMa <- t(logNormExpresMa)
  rownames(logNormExpresMa) <- gsub("^.*?\\.","",rownames(logNormExpresMa))
  
  ## remove genes if they are all the same in all groups
  ind <- apply(logNormExpresMa, 1, sd) == 0
  logNormExpresMa <- logNormExpresMa[!ind,]
  genes <- genes[!ind,]

  ## color columns according to cluster
  annotation_col <- as.data.frame(gsub("(^.*?_)","",
                                       colnames(logNormExpresMa)))%>%
    dplyr::mutate(celltype=gsub("(_.*$)","",colnames(logNormExpresMa)))
  colnames(annotation_col)[1] <- "col1"
  annotation_col <- annotation_col %>%
    dplyr::mutate(cond = gsub("(^[0-9]_?)","",col1)) %>%
    dplyr::select(cond, celltype)
  rownames(annotation_col) <- colnames(logNormExpresMa) 

  ann_colors = list(
      celltype=colVec)
  if(is.null(ann_colors$cond)){
    annotation_col$cond <- NULL
  }
  
  ## adjust order
  logNormExpresMa <- logNormExpresMa[selGenes,]
  if(is.null(ordVec)){
  ordVec <- levels(seuratS)
  ordVec <-c ("0", "1", "2", "3","4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19")
  }
  logNormExpresMa <- logNormExpresMa[,ordVec]

  ## scaled row-wise
  pheatmap(logNormExpresMa, scale="row" ,treeheight_row = 0,cluster_rows = cr, 
         cluster_cols = cc, color = colorRampPalette(c("#2166AC", "#F7F7F7", "#B2182B"))(50),
         annotation_col = annotation_col, cellwidth=15, cellheight=10,
         annotation_colors = ann_colors, gaps_row = gapVecR, gaps_col = gapVecC)
}

genesPlot <- data.frame(gene=c("TTN", "MYBPC3", "RYR2", "NEBL", "TNNT2", "CMYA5", "COL6A3", "DCN",  "FBN1", "C7", "PDGFRA", "CDH19", "PDGFRB","ITGA7","RGS5", "NOTCH3", "MYH11", "ACTA2","PECAM1", "VWF", "EGFL7", "POSTN", "ITGA10", "CDH11","CCL21", "PROX1", "FLT4", "NRXN1", "ANK3", "PTPRZ1", "ASIC2", "PIEZO2", "MTHFD1L", "ACACB", "PLIN1", "GPAM", "CD163", "MRC1", "SIGLEC1", "STAB1", "CSF1R", "MERTK", "IL7R", "PTPRC", "CD2", "KIT", "CPA3", "IL18R1", "MS4A1", "PAX5", "FCRL5","IGKC"))

levels(seuratS)
##  [1] "2"  "0"  "6"  "3"  "1"  "11" "4"  "13" "8"  "17" "7"  "9"  "5"  "12" "19"
## [16] "15" "14" "16" "18" "10"
colVec <- colclusterName

avgHeatmap(seuratS, selGenes = genesPlot, colVecIdent = colVec)